package com.vipshop.view;



import java.util.HashMap;

import com.vipshop.util.StringHelper;

 

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.Toast;
import android.widget.PopupWindow.OnDismissListener;

public class WindowBuilder{

	private LayoutInflater mInflater;
	private PopupWindow mPopupWindow;
	private int mGravity;
	private View mParent;
	private int mAnimation;
	private HashMap<Integer, PopupWindow> mViewMap;
	private int mWidth;
	private int mHeight;
	private View mBackground;

	/**
	 * @param context
	 * @param parent parent views with showAtLocation()
	 * @param gravity
	 * @param x
	 * @param y
	 * @param animation animation style
	 */
	public WindowBuilder(Context context,View parent,int gravity,int width, int height,int background,int animation){
		mInflater = LayoutInflater.from(context);
		mAnimation = animation;
		mParent = parent;
		mGravity = gravity;
		mWidth = width;
		mHeight = height;
		mViewMap = new HashMap<Integer, PopupWindow>();
		mBackground= new View(context);
		mBackground.setBackgroundColor(background);
		mBackground.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		mBackground.setVisibility(View.GONE);
		((ViewGroup)parent).addView(mBackground);
	}


	/**
	 * @param action identification code
	 * @param adapter
	 * @param useCache true use the cacehe view
	 * @param params
	 */
	public void show(int action,WindowAdapter adapter,boolean useCache,Object...params){
		
		dismiss();

		mBackground.setVisibility(View.VISIBLE);

		if(useCache)mPopupWindow = getViewMap(action);

		if(!StringHelper.isNull(mPopupWindow)){
			mPopupWindow = adapter.getWindow(action, mInflater, mWidth, mHeight, params);
			if(useCache)mViewMap.put(action, mPopupWindow);
		}

		if(!StringHelper.isNull(mPopupWindow)){
			setWindowState(mPopupWindow);
			mPopupWindow.setOnDismissListener(new OnWindowDismissListener(action, mPopupWindow,
					adapter, params) );
			adapter.onShowWindow(action,mPopupWindow,params);
			mPopupWindow.showAtLocation(mParent,mGravity,0,0);
		}
	}


	protected void setWindowState(PopupWindow window) {
		window.setBackgroundDrawable(new BitmapDrawable());
		window.setAnimationStyle(mAnimation);
		window.setFocusable(true);
		window.setTouchable(true);
		window.setOutsideTouchable(true);
		window.update();
	}

	public void dismiss(){
		if(mPopupWindow != null){
			mPopupWindow.dismiss();
		}
		mPopupWindow = null;
	}

	public interface WindowAdapter{

		/**
		 * do something before show window
		 * @param action
		 * @param window
		 * @param params
		 */
		public void onShowWindow(int action, PopupWindow window,Object...params);

		/**
		 * new a window
		 * @param action
		 * @param inflater
		 * @param defaultWidth
		 * @param defaultHeight
		 * @param params
		 * @return
		 */
		public PopupWindow getWindow(int action,LayoutInflater inflater, int defaultWidth ,
				int defaultHeight,Object...params);


		/**
		 * do something when dismiss window
		 * @param action
		 * @param window
		 * @param params
		 */
		public void onDismissWindow(int action, PopupWindow window,Object...params);

	}

	private PopupWindow getViewMap(Integer position){

		if(mViewMap.containsKey(position)){

			return mViewMap.get(position);

		}else{

			return null;
		}

	}

	private class OnWindowDismissListener implements OnDismissListener{

		private WindowAdapter mAdapter;
		private int mAction;
		private PopupWindow mWindow;
		private Object[] mParams;

		public OnWindowDismissListener(int action,PopupWindow window, WindowAdapter adapter,
				Object[] params) {
			super();
			mAdapter = adapter;
			mAction = action;
			mWindow = window;
			mParams = params;
		}

		@Override
		public void onDismiss() {
			if(mBackground.getVisibility() == View.VISIBLE){
				mBackground.setVisibility(View.GONE);
			}
			if(!StringHelper.isNull(mAdapter)){
				mAdapter.onDismissWindow(mAction, mWindow, mParams);
			}
		}

	}

	public void clear() {
		dismiss();
		mViewMap.clear();
		mViewMap = null;
	}
	
}
